package com.cskaoyan.service.market;

import com.cskaoyan.bean.market.Order;
import com.cskaoyan.bean.market.OrderExample;
import com.cskaoyan.bean.market.Order_goods;
import com.cskaoyan.bean.market.Order_goodsExample;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bo.market.OrderRefundBo;
import com.cskaoyan.bo.market.OrderShipBo;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.Order_goodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.utils.Converter;
import com.cskaoyan.utils.PojoUtils;
import com.cskaoyan.vo.market.OrderDetailDataVo;
import com.cskaoyan.vo.market.OrderListDataVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-09 23:08
 **/

// 订单
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    Order_goodsMapper order_goodsMapper;


    @Override
    public OrderListDataVo orderList(Integer page, Integer limit, String sort, String order,
                                     Integer userId, String orderSn, Short[] orderStatusArray) {
        // 分页，排序
        PageHelper.startPage(page, limit);

        // 查询订单数据 并排序
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(sort + " " + order);
        OrderExample.Criteria criteria = orderExample.createCriteria().andDeletedEqualTo(false);
        // 条件查询
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (orderSn != null && !"".equals(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        if (orderStatusArray != null && orderStatusArray.length != 0) {
            ArrayList<Short> shorts = new ArrayList<>();
            shorts.addAll(Arrays.asList(orderStatusArray));
            criteria.andOrderStatusIn(shorts);
        }
        List<Order> orders = orderMapper.selectByExample(orderExample);

        // 转换
        ArrayList<OrderListDataVo.ItemsBean> itemsBeans = new ArrayList<>();
        Converter converter = PojoUtils.converter();
        converter.listConverter(orders,itemsBeans,OrderListDataVo.ItemsBean.class);

        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        long total = orderPageInfo.getTotal();

        return new OrderListDataVo(total, itemsBeans);
    }

    @Override
    public OrderDetailDataVo orderDetail(Integer id) {

        // 查询订单信息
        Order order = orderMapper.selectByPrimaryKey(id);
        // 查询用户信息
        User user = userMapper.selectByPrimaryKey(order.getUserId());
        // 查询商品信息(根据订单 id 查询相关的商品信息)
        Order_goodsExample order_goodsExample = new Order_goodsExample();
        order_goodsExample.createCriteria().andOrderIdEqualTo(order.getId());
        List<Order_goods> order_goodsList = order_goodsMapper.selectByExample(order_goodsExample);


        // 相应参数设置
        // 订单参数设置
        Converter converter = PojoUtils.converter();
        OrderDetailDataVo.OrderBean orderBean = new OrderDetailDataVo.OrderBean();
        converter.converter(order, orderBean);
        // 用户参数设置
        OrderDetailDataVo.UserBean userBean = new OrderDetailDataVo.UserBean(user.getNickname(), order.getAddress());
        // 商品参数设置
        ArrayList<OrderDetailDataVo.OrderGoodsBean> orderGoodsBeans = new ArrayList<>();
        Converter converter1 = PojoUtils.converter();
        converter1.listConverter(order_goodsList,orderGoodsBeans,OrderDetailDataVo.OrderGoodsBean.class);

        return new OrderDetailDataVo(userBean,orderBean,orderGoodsBeans);
    }

    @Override
    public Order orderRefund(OrderRefundBo refundBo) {
        Order order = new Order();
        order.setId(refundBo.getOrderId());
        // 修改订单状态
        order.setOrderStatus((short) 203);
        order.setUpdateTime(new Date());
        orderMapper.updateByPrimaryKeySelective(order);
        // 查询该订单
        Order order1 = orderMapper.selectByPrimaryKey(refundBo.getOrderId());
        return order1;
    }

    @Override
    public Order orderShip(OrderShipBo orderShipBo) {
        Order order = new Order();
        order.setId(orderShipBo.getOrderId());
        order.setShipSn(orderShipBo.getShipSn());
        order.setShipChannel(orderShipBo.getShipChannel());
        // 修改订单状态
        order.setUpdateTime(new Date());
        order.setOrderStatus((short) 301);
        orderMapper.updateByPrimaryKeySelective(order);
        // 查询该订单
        Order order1 = orderMapper.selectByPrimaryKey(orderShipBo.getOrderId());
        return order1;
    }
}
